package com.youkeyi.ddy.cloud.basic.pub.corp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.corp.entity.SysCorpSort;
import com.youkeyi.ddy.cloud.basic.pub.corp.service.SysCorpSortService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpSortCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 主体显示排序表
 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-05-23
 */
@Service
public class SysCorpSortController implements PublicCorpSortCloudService {

    @Autowired
    private SysCorpSortService sysCorpSortService;

    @Override
    public PublicCorpSortListCloudDto query(PublicCorpSortQueryCloudParam param) {
        PublicCorpSortListCloudDto dto = new PublicCorpSortListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCorpPrimaryKeys())) {
            param.getCorpPrimaryKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysCorpSort> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCorpPrimaryKeys()), SysCorpSort::getCorpKey, param.getCorpPrimaryKeys())
                .ne(SysCorpSort::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysCorpSort> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysCorpSort> corpList = sysCorpSortService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(corpList.getRecords())) {
            dto.setTotal(corpList.getTotal());

            List<PublicCorpSortListItemCloudDto> items = new ArrayList<>();
            corpList.getRecords().forEach(corp -> {
                PublicCorpSortListItemCloudDto item = new PublicCorpSortListItemCloudDto();
                item.setCorpSortPrimaryKey(corp.getPrimaryKey());
                item.setCorpKey(corp.getCorpKey());
                item.setMappingCorpKey(corp.getMappingCorpKey());
                item.setSort(corp.getSort());
                item.setRemark(corp.getRemark());
                item.setIsDeleted(corp.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(corp.getCreateTime() != null ? corp.getCreateTime().getTime() : null);
                item.setCreateUserKey(corp.getCreateUserKey());
                item.setCreateUserName(corp.getCreateUserName());
                item.setUpdateTime(corp.getUpdateTime() != null ? corp.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(corp.getUpdateUserKey());
                item.setUpdateUserName(corp.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicCorpSortCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getMappingCorpKey()) || null == param.getSort()) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysCorpSort corp = new SysCorpSort();
        corp.setPrimaryKey(SimpleUUidUtils.getUuid());
        corp.setCorpKey(param.getCorpKey());
        corp.setMappingCorpKey(param.getMappingCorpKey());
        corp.setSort(param.getSort());
        corp.setRemark(param.getRemark());
        corp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        corp.setCreateUserKey(param.getCreateUserKey());
        corp.setCreateUserName(param.getCreateUserName());
        corp.setCreateTime(new Date());
        boolean save = sysCorpSortService.save(corp);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(corp.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(PublicCorpSortUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCorpSortPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysCorpSort> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysCorpSort::getPrimaryKey, param.getCorpSortPrimaryKey());
        SysCorpSort corp = new SysCorpSort();
        corp.setPrimaryKey(param.getCorpSortPrimaryKey());
        corp.setCorpKey(param.getCorpKey());
        corp.setMappingCorpKey(param.getMappingCorpKey());
        corp.setSort(param.getSort());
        corp.setRemark(param.getRemark());
        corp.setUpdateUserKey(param.getUpdateUserKey());
        corp.setUpdateUserName(param.getUpdateUserName());
        corp.setUpdateTime(new Date());
        boolean save = sysCorpSortService.update(corp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(PublicCorpSortDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCorpSortPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysCorpSort> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysCorpSort::getPrimaryKey, param.getCorpSortPrimaryKeys());

        SysCorpSort corp = new SysCorpSort();
        corp.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        corp.setUpdateUserKey(param.getUpdateUserKey());
        corp.setUpdateUserName(param.getUpdateUserName());
        corp.setUpdateTime(new Date());
        boolean save = sysCorpSortService.update(corp, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
